package java8.completablefuture;

/**
 * @program: util
 * @description:
 * @author: zhouliangliang
 * @create: 2019-09-27 20:39
 **/
public class Discount {
    public enum Code{
        NONE(0), SILVER(5), GOLD(10), PLATINUM(15), DIAMOND(20);
        private final int percentage;

        Code(int percentage) {
            this.percentage = percentage;
        }
    }

    public static String applyDiscount(Quote quote) {
        return quote.getShopName() + " price is " + Discount.apply(quote.getPrice(), quote.getDiscountCode());
    }

    private static double apply(double price, Code code) {
        delay();
        return Double.valueOf(String.format("%.2f", price * (100 - code.percentage) / 100));
    }

    private static void delay() {
        try {
            Thread.sleep(1000l);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
